<!-- 用户管理 -->
<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="关键字" prop="keywords">
          <el-input v-model="queryParams.keywords" placeholder="用户名/昵称/手机号" clearable style="width: 200px"
            @keyup.enter="handleQuery" />
        </el-form-item>

        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="全部" clearable class="!w-[100px]">
            <el-option label="正常" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>

        <el-form-item label="创建时间">
          <el-date-picker v-model="queryParams.createTime" :editable="false" class="!w-[240px]" type="daterange"
            range-separator="~" start-placeholder="开始时间" end-placeholder="截止时间" value-format="YYYY-MM-DD" />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="search" @click="handleQuery">搜索</el-button>
          <el-button icon="refresh" @click="handleResetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="flex-x-between bg-white p-2">
      <div>
        <el-button v-hasPerm="['sys:user:add']" type="success" icon="plus" @click="handleOpenDialog()">
          新增
        </el-button>
        <el-button v-hasPerm="'sys:user:delete'" type="danger" icon="delete" :disabled="selectIds.length === 0"
          @click="handleDelete()">
          删除
        </el-button>
      </div>
    </div>
    <el-card shadow="never" class="flex-1 flex flex-col">
      <el-table v-loading="loading" :data="pageData" @selection-change="handleSelectionChange" height="100%"
        class="flex-1">
        <el-table-column type="selection" width="50" align="center" />
        <el-table-column label="用户名" prop="username" />
        <el-table-column label="昵称" align="center" prop="nickname" />
        <el-table-column label="性别" align="center">
          <template #default="scope">
            <!-- 性别字典翻译 -->
            <DictLabel v-model="scope.row.gender" code="gender" />
          </template>
        </el-table-column>
        <el-table-column label="用户类型" align="center" prop="userType">
          <template #default="{ row }">
            <span>{{ getLabelByValue(typeOptions, row.userType) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号码" align="center" prop="mobile" />
        <!-- <el-table-column label="邮箱" align="center" prop="email" width="160" /> -->
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <el-tag :type="scope.row.status == 1 ? 'success' : 'info'">
              {{ scope.row.status == 1 ? "正常" : "禁用" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" />
        <el-table-column label="操作" fixed="right" width="240">
          <template #default="scope">
            <el-button v-hasPerm="'sys:user:password:reset'" type="primary" icon="RefreshLeft" size="small" link
              @click="hancleResetPassword(scope.row)">
              重置密码
            </el-button>
            <el-button v-hasPerm="'sys:user:edit'" type="primary" icon="edit" link size="small"
              @click="handleOpenDialog(scope.row.id)">
              编辑
            </el-button>
            <el-button v-hasPerm="'sys:user:delete'" type="danger" icon="delete" link size="small"
              @click="handleDelete(scope.row.id)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div class="bg-white">
      <pagination v-if="total > 0" v-model:total="total" v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize" @pagination="handleQuery" />
    </div>
    <!-- <el-col :lg="4" :xs="24" class="mb-[12px]">
        <DeptTree v-model="queryParams.deptId" @node-click="handleQuery" />
      </el-col> -->
    <!-- 用户表单 -->
    <el-drawer v-model="dialog.visible" :title="dialog.title" append-to-body @close="handleCloseDialog">
      <el-form ref="userFormRef" :model="formData" :rules="rules" label-width="100">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="formData.username" :readonly="!!formData.id" placeholder="请输入用户名" />
        </el-form-item>

        <el-form-item label="用户昵称" prop="nickname">
          <el-input v-model="formData.nickname" placeholder="请输入用户昵称" />
        </el-form-item>

        <!-- <el-form-item label="所属部门" prop="deptId">
          <el-tree-select
            v-model="formData.deptId"
            placeholder="请选择所属部门"
            :data="deptOptions"
            filterable
            check-strictly
            :render-after-expand="false"
          />
        </el-form-item> -->

        <el-form-item label="性别" prop="gender">
          <Dict v-model="formData.gender" code="gender" />
        </el-form-item>
        <el-form-item label="类型" prop="userType">
          <el-select v-model="formData.userType" placeholder="请选择">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="高级合伙人" prop="pid" v-if="formData.userType === '11'">
          <el-select v-model="formData.pid" placeholder="请选择高级合伙人" clearable>
            <el-option v-for="item in usersOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="角色" prop="roleIds">
          <el-select v-model="formData.roleIds" multiple placeholder="请选择">
            <el-option v-for="item in roleOptions" :key="item.value" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="formData.mobile" placeholder="请输入手机号码" maxlength="11" />
        </el-form-item>

        <!-- <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" placeholder="请输入邮箱" maxlength="50" />
        </el-form-item> -->

        <el-form-item label="状态" prop="status">
          <el-switch v-model="formData.status" inline-prompt active-text="正常" inactive-text="禁用" :active-value="1"
            :inactive-value="0" />
        </el-form-item>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
          <el-button @click="handleCloseDialog">取 消</el-button>
        </div>
      </template>
    </el-drawer>

    <!-- 用户导入 -->
    <UserImport v-model="importDialogVisible" @import-success="handleQuery()" />
  </div>
</template>

<script setup lang="ts">
import UserAPI, { UserForm, UserPageQuery, UserPageVO } from "@/api/system/user";

import DeptAPI from "@/api/system/dept";
import RoleAPI from "@/api/system/role";

import DeptTree from "./components/DeptTree.vue";
import UserImport from "./components/UserImport.vue";
import { getLabelByValue, getLabelValueList } from "../../../utils/enums";
import { USER_TYPE_ENUM } from "../../../enums/user/UserEnum";
import MerchantUserAPI from "@/api/user/merchantuser";

defineOptions({
  name: "User",
  inheritAttrs: false,
});
const queryFormRef = ref();
const userFormRef = ref();

const queryParams = reactive<UserPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

const pageData = ref<UserPageVO[]>();
const total = ref(0);
const loading = ref(false);

const dialog = reactive({
  visible: false,
  title: "新增用户",
});

const formData = reactive<UserForm>({
  status: 1,
  userType: "",
  pid: undefined,
});
function validatePid(rule, value, callback: Function) {
  if (formData.userType === "11" && !value) {
    callback(new Error("高级合伙人不能为空"));
  } else {
    callback();
  }
}
const rules = reactive({
  username: [
    { max: 30, message: "不能超过30个字符", trigger: "blur" },
    { required: true, message: "用户名不能为空", trigger: "blur" },
  ],
  nickname: [
    { max: 30, message: "不能超过30个字符", trigger: "blur" },
    { required: true, message: "用户昵称不能为空", trigger: "blur" },
  ],
  // deptId: [{ required: true, message: "所属部门不能为空", trigger: "blur" }],
  roleIds: [{ required: true, message: "用户角色不能为空", trigger: "blur" }],
  userType: [{ required: true, message: "用户类型不能为空", trigger: "change" }],
  pid: [
    { required: true, message: "高级合伙人不能为空", trigger: "blur" },
    {
      validator: validatePid,
      trigger: "change",
    },
  ],
  email: [
    {
      pattern: /\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}/,
      message: "请输入正确的邮箱地址",
      trigger: "blur",
    },
  ],
  mobile: [
    { required: true, message: "手机号码不能为空", trigger: "blur" },
    {
      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      message: "请输入正确的手机号码",
      trigger: "blur",
    },
  ],
});

// 选中的用户ID
const selectIds = ref<number[]>([]);
// 部门下拉数据源
const deptOptions = ref<OptionType[]>();
// 角色下拉数据源
const roleOptions = ref<OptionType[]>();
const typeOptions = [
  {
    label: "平台方",
    value: "P01",
  },
  {
    label: "渠道合伙人",
    value: "P02",
  },
  {
    label: "高级合伙人",
    value: "P03",
  },
];
// 导入弹窗显示状态
const importDialogVisible = ref(false);
const usersOptions = ref([]);

// 查询
function handleQuery() {
  loading.value = true;
  UserAPI.getPage(queryParams)
    .then((data: any) => {
      pageData.value = data.data;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

// 重置查询
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  // queryParams.deptId = undefined;
  queryParams.createTime = undefined;
  handleQuery();
}

// 选中项发生变化
function handleSelectionChange(selection: any[]) {
  selectIds.value = selection.map((item) => item.id);
}

// 重置密码
function hancleResetPassword(row: UserPageVO) {
  ElMessageBox.prompt("请输入用户【" + row.username + "】的新密码", "重置密码", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
  }).then(
    ({ value }) => {
      if (!value || value.length < 6) {
        ElMessage.warning("密码至少需要6位字符，请重新输入");
        return false;
      }
      UserAPI.resetPassword(row.id, value).then(() => {
        ElMessage.success("密码重置成功，新密码是：" + value);
      });
    },
    () => {
      ElMessage.info("已取消重置密码");
    }
  );
}

/**
 * 打开弹窗
 *
 * @param id 用户ID
 */
async function handleOpenDialog(id?: number) {
  getUsersOptions();
  dialog.visible = true;
  // 加载角色下拉数据源
  await RoleAPI.getOptions().then((res: any) => {
    roleOptions.value = res.data;
  });
  // 加载部门下拉数据源
  // const deptOptions = await DeptAPI.getOptions();
  // deptOptions.value = deptOptions.data;
  if (id) {
    dialog.title = "修改用户";
    UserAPI.getFormData(id).then((data) => {
      console.log(data);

      Object.assign(formData, { ...data.data });
    });
  } else {
    dialog.title = "新增用户";
  }
}

// 关闭弹窗
function handleCloseDialog() {
  dialog.visible = false;
  userFormRef.value.resetFields();
  userFormRef.value.clearValidate();

  formData.id = undefined;
  formData.status = 1;
}

// 提交用户表单（防抖）
const handleSubmit = useDebounceFn(() => {
  userFormRef.value.validate((valid: boolean) => {
    if (valid) {
      const userId = formData.id;
      loading.value = true;
      if (userId) {
        UserAPI.update(userId, formData)
          .then(() => {
            ElMessage.success("修改用户成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        UserAPI.add(formData)
          .then(() => {
            ElMessage.success("新增用户成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}, 1000);

/**
 * 删除用户
 *
 * @param id  用户ID
 */
function handleDelete(id?: number) {
  const userIds = [id || selectIds.value].join(",");
  if (!userIds) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除用户?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    function () {
      loading.value = true;
      UserAPI.deleteByIds(userIds)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    function () {
      ElMessage.info("已取消删除");
    }
  );
}

// 打开导入弹窗
function handleOpenImportDialog() {
  importDialogVisible.value = true;
}

// 导出用户
function handleExport() {
  UserAPI.export(queryParams).then((response: any) => {
    const fileData = response.data;
    const fileName = decodeURI(response.headers["content-disposition"].split(";")[1].split("=")[1]);
    const fileType =
      "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";

    const blob = new Blob([fileData], { type: fileType });
    const downloadUrl = window.URL.createObjectURL(blob);

    const downloadLink = document.createElement("a");
    downloadLink.href = downloadUrl;
    downloadLink.download = fileName;

    document.body.appendChild(downloadLink);
    downloadLink.click();

    document.body.removeChild(downloadLink);
    window.URL.revokeObjectURL(downloadUrl);
  });
}
async function getUsersOptions() {
  try {
    const data = await MerchantUserAPI.getUsersOptions({
      types: USER_TYPE_ENUM.SENIOR_PARTNER.value,
    });
    usersOptions.value = data.data;
  } catch (e) {
    console.log(e);
  } finally {
  }
}
onMounted(() => {
  handleQuery();
  getUsersOptions();
});
</script>
<style lang="scss" scoped>
:deep(.el-card__body) {
  height: 100%;
}
</style>
